﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class BaseTab : UserControl
    {
        public BaseTab()
        {
            InitializeComponent();

            baseButtonPanel1.OkClick += OnSettingRequest;
            baseButtonPanel1.ResetClick += OnResetRequest;
            BaseTabControl.Dock = DockStyle.Fill; //ps dock baseTabControl
        }


        internal event SettingRequestHandler SettingRequest;
        internal event ResetRequestHandler ResetRequest;


        public TabControl BaseTc => BaseTabControl;


        private TabControl.TabPageCollection TcTabPages
        {
            get { return BaseTc.TabPages; }
        }

        private void SetForRange()
        {
            TcTabPages.Clear();
            TcTabPages.AddRange(PagesForRangeExplorer());
        }

        private static TabPage[] PagesForRangeExplorer()
        {
            TabPage numberFormat = new TabPage("数字格式") {Name = "NumberFormat"};
            TabPage alignment = new TabPage("对齐") {Name = "Alignment"};
            TabPage font = new TabPage("字体") {Name = "Font"};
            TabPage border = new TabPage("边框") {Name = "Border"};
            TabPage interior = new TabPage("填充") {Name = "Interior"};
            TabPage protection = new TabPage("保护") {Name = "Protection"};
            TabPage hyperlink = new TabPage("超链接") {Name = "Hyperlink"};
            TabPage validation = new TabPage("数据验证") {Name = "Validation"};
            TabPage conditionalFormats = new TabPage("条件格式") {Name = "ConditionalFormats"};

            TabPage[] array =
            {
                numberFormat, alignment, font, border, interior, protection, hyperlink, validation, conditionalFormats
            };

            return array;
        }

        internal void SetTabPage(TabPage[] pageToSet)
        {
            BaseTabControl.TabPages.Clear();
            BaseTabControl.TabPages.AddRange(pageToSet);
        }

        public void SetChildTabControlIndex(int index = 0)
        {
            BaseTabControl.SelectedIndex = index;
        }

        internal void HideButtonPanel(IEnumerable<int> hideIndex)
        {
            if (hideIndex.Contains(BaseTabControl.SelectedIndex))
            {
                if (baseButtonPanel1.Visible)
                    baseButtonPanel1.Visible = false;
                baseButtonPanel1.Enabled = false;
            }
            else
            {
                if (!baseButtonPanel1.Visible)
                    baseButtonPanel1.Visible = true;
                baseButtonPanel1.Enabled = true;
            }
        }

        protected virtual void OnSettingRequest(object sender, EventArgs e)
        {
            SettingRequest?.Invoke(sender, e);
        }

        protected virtual void OnResetRequest(object sender, EventArgs e)
        {
            ResetRequest?.Invoke(sender, e);
        }
    }
}